package com.hopu.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        // 在拦截点执行前拦截，如果返回true则不执行拦截点后的操作（拦截成功）
        // 返回false则不执行拦截
//        String url = request.getRequestURI(); // 获取登录的uri，这个是不进行拦截的
//        String ename = (String) request.getSession().getAttribute("ename");
//        if (ename!=null && ename!=""){
//            System.out.println("放行");
//            return true;
//        }
//        System.out.println("拦截");
//        response.sendRedirect(request.getContextPath()+"/index.jsp");
//        return false;

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        //处理过程中执行
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        //执行完毕，返回前拦截
    }
}
